package com.lineage.server.datatables.sql;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.storage.LogEnchantStorage;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LogEnchantTable implements LogEnchantStorage {
  private static final Log _log = LogFactory.getLog(LogEnchantTable.class);
  
  public void failureEnchant(L1PcInstance pc, L1ItemInstance item) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement(
          "INSERT INTO `character_角色_冲装1` SET `item_id`=?,`char_id`=?,`item_name`=?,`count`=?,`is_equipped`=?,`enchantlvl`=?,`is_id`=?,`durability`=?,`charge_count`=?,`remaining_time`=?,`last_used`=?,`bless`=?,`attr_enchant_kind`=?,`attr_enchant_level`=?,`datetime`=SYSDATE(),`ipmac`=?");
      int i = 0;
      ps.setInt(++i, item.getItem().getItemId());
      ps.setInt(++i, pc.getId());
      ps.setString(++i, item.getItem().getName());
      ps.setLong(++i, item.getCount());
      ps.setInt(++i, 0);
      ps.setInt(++i, item.getEnchantLevel());
      ps.setInt(++i, item.isIdentified() ? 1 : 0);
      ps.setInt(++i, item.get_durability());
      ps.setInt(++i, item.getChargeCount());
      ps.setInt(++i, item.getRemainingTime());
      ps.setTimestamp(++i, item.getLastUsed());
      ps.setInt(++i, item.getBless());
      ps.setInt(++i, item.getAttrEnchantKind());
      ps.setInt(++i, item.getAttrEnchantLevel());
      StringBuilder ip = pc.getNetConnection().getIp();
      StringBuilder mac = pc.getNetConnection().getMac();
      ps.setString(++i, ip + "/" + mac);
      ps.execute();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  public void resetEnchant(L1PcInstance pc, L1ItemInstance item) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("DELETE FROM `character_角色_冲装1` WHERE `id`=?");
      ps.setInt(1, item.getId());
      ps.execute();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
}
